package proxy;

/**
 * Created by zhoum on 2019-06-27.
 */
public class StaticProxy {

    public static void main(String[] args) {
//静态代理
        System.out.println("静态代理-----------------------------------");
        Calculator calculator = new CalculatorImpl();
        StaticProxy staticProxy = new StaticProxy(calculator);
        System.out.println(staticProxy.add(1, 2));
        System.out.println(staticProxy.subtract(1, 2));
    }


    private Calculator target;

    public StaticProxy(Calculator calculator) {
        this.target = calculator;
    }

    public int add(int a, int b) {
        System.out.println("add method to start");
        int i = target.add(a, b);
        System.out.println("add method to end");
        return i;
    }

    public int subtract(int a, int b) {
        System.out.println("subtract method to start");
        int i = target.subtract(a, b);
        System.out.println("subtract method to end");
        return i;
    }


    public int divide(int a, int b) {
        System.out.println("divide method to start");
        int i = target.subtract(a, b);
        System.out.println("divide method to end");
        return i;
    }


    public int multiply(int a, int b) {
        System.out.println("multiply method to start");
        int i = target.subtract(a, b);
        System.out.println("multiply method to end");
        return i;
    }

    /**
     * Created by zhoum on 2019-06-27.
     */
    public interface Calculator {
        int add(int a, int b);

        int subtract(int a, int b);
    }


    /**
     * Created by zhoum on 2019-06-27.
     */
    public interface Calculator2 {
        int divide(int a, int b);

        int multiply(int a, int b);
    }

    /**
     * Created by zhoum on 2019-06-27.
     */
    public static class CalculatorImpl implements Calculator, Calculator2 {

        @Override
        public int add(int a, int b) {
            return a + b;
        }

        @Override
        public int subtract(int a, int b) {
            return a - b;
        }

        @Override
        public int divide(int a, int b) {
            return a / b;
        }

        @Override
        public int multiply(int a, int b) {
            return a * b;
        }
    }
}
